package com.yc.cloudvideo.sp_user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.cloudvideo.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
public interface UserMapper extends BaseMapper<User> {

    /**
     * 新增账号
     * @param user
     * @return
     */
    @Insert("insert into user(email,password,salt,confirm_code,activation_time,exist) " +
            "  VALUE(#{email},#{password},#{salt},#{confirmCode},#{activationTime},#{exist})")
    int insertUser(User user);

    /**
     * 根据确认码查询用户
     * @param confirmCode
     * @return
     */
    @Select("select email,activation_time from user where " +
            "confirm_code=#{confirmCode} and exist=0")
    User selectUserByConfirmCode(@Param("confirmCode") String confirmCode);

    /**
     * 根据确认码查询用户并修改状态值为 1 (可用)
     * @param confirmCode
     * @return
     */
    @Update("update user set exist=1 where confirm_code=#{confirmCode}")
    int updateUserByConfirmCode(@Param("confirmCode") String confirmCode);

    /**
     * 根据邮箱查询用户
     * @param email
     * @return
     */
    @Select("select email,password,salt from user where email=#{email} and exist=1")
    List<User> selectUserByEmail(@Param("email") String email);
    /**==================================================================================*/
    @Select("select u.uid,u.head,u.nick_name,u.sign,u.email,u.is_boy,u.age,u.reg_time,u.vip" +
            " from store_user s join user u on s.uid=u.uid  where cid=#{cid} and level=1")
    User selectStoreOwner(int cid);

    //不查密码
    @Select("select uid,head,nick_name,sign,email,is_boy,age,reg_time,vip from user where uid=#{uid}")
    User selectUserByUid(int uid);

    //根据cid查询出当前仓库的贡献者信息
    @Select("SELECT\n" +
            "	u.*\n" +
            "FROM\n" +
            "	store_file s\n" +
            "JOIN file f ON s.fid = f.fid\n" +
            "JOIN USER u ON u.uid = f.uid\n" +
            "WHERE\n" +
            "	cid = #{cid}\n" +
            "GROUP BY\n" +
            "	f.uid")
    List<User> selectUserGByCid(int cid);

    /**
     * 登录成功后修改注册时间
     */
    @Update("update user set reg_time=SYSDATE() where email=#{email}")
    int updateUserByEmail(@Param("email") String email);

    /**
     * 根据邮箱查询用户
     * @param email
     * @return
     */
    @Select("select uid,email,password,salt,reg_time from user where email=#{email}")
    Map<String,Object> selectUserByEmailAfter(@Param("email") String email);

    /**
     * 修改用户信息
     * @param nickName
     * @param sign
     * @param isBoy
     * @param age
     * @param uid
     * @return
     */
    @Update("update user set nick_name=#{nickName},sign=#{sign},is_boy=#{isBoy},age=#{age} where uid=#{uid}")
    int updateUserInfo(@Param("nickName") String nickName,@Param("sign") String sign,@Param("isBoy") boolean isBoy,
                       @Param("age") Integer age,@Param("uid") String uid);

    /**
     * 修改密码
     * @param password
     * @param uid
     * @return
     */
    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePwd(@Param("password") String password,@Param("uid") String uid);

    @Select("select * from user where uid=#{uid}")
    User selectByUid(@Param("uid")String uid);
}
